package com.minglv.mini.service.impl;

import com.minglv.mini.domain.UserInformation;
import com.minglv.mini.domain.dto.InformationDto;
import com.minglv.mini.mapper.UserInformationMapper;
import com.minglv.mini.service.IUserInformationService;
import com.minglv.mini.tools.JWTUtils;
import com.minglv.mini.tools.MiniResult;
import com.ruoyi.common.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInformationServiceImpl implements IUserInformationService {

    @Autowired
    private UserInformationMapper userInformationMapper;


    @Override
    public Map getInformation(String token) {

        Long userId = JWTUtils.getUserid(token);

        UserInformation userInformation = new UserInformation();
        userInformation.setUserId(userId);
        List<UserInformation> userInformations = userInformationMapper.selectUserInformationList(userInformation);
        Map reMap = new HashMap();
        if (!CollectionUtils.isEmpty(userInformations)) {
            UserInformation userInformation1 = userInformations.get(0);
            reMap.put("realname",userInformation1.getRealname());
            reMap.put("phone",userInformation1.getPhone());
            reMap.put("company_name",userInformation1.getCompanyName());
            reMap.put("position",userInformation1.getPosition());
            reMap.put("email",userInformation1.getEmail());
        }else{
            reMap.put("realname","");
            reMap.put("phone","");
            reMap.put("company_name","");
            reMap.put("position","");
            reMap.put("email","");
        }
        return reMap;
    }

    @Override
    public Object postInformation(String token, InformationDto informationDto) {
        token = token.replace(Constants.TOKEN_PREFIX, "");
        Long userId = JWTUtils.getUserid(token);
        UserInformation se = new UserInformation();
        se.setUserId(userId);
        List<UserInformation> userInformations = userInformationMapper.selectUserInformationList(se);
        if (CollectionUtils.isEmpty(userInformations)) {
            UserInformation ui = new UserInformation();
            Long maxId = userInformationMapper.getMaxId();
            ui.setId(maxId + 1);
            ui.setUserId(userId);
            ui.setCompanyName(informationDto.getCompany_name());
            ui.setPhone(informationDto.getPhone());
            ui.setEmail(informationDto.getEmail());
            ui.setPosition(informationDto.getPosition());
            ui.setRealname(informationDto.getRealname());
            ui.setStatus(1);
            userInformationMapper.insertUserInformation(ui);
        }else{
            UserInformation userInformation = userInformations.get(0);
            userInformation.setPosition(informationDto.getPosition());
            userInformation.setEmail(informationDto.getEmail());
            userInformation.setUserId(userId);
            userInformation.setCompanyName(informationDto.getCompany_name());
            userInformation.setPhone(informationDto.getPhone());
            userInformation.setRealname(informationDto.getRealname());
            userInformationMapper.updateUserInformation(userInformation);
        }

        return new MiniResult(200,"保存成功",null);
    }

    /**
     * 查询投诉列
     *
     * @param id 投诉列主键
     * @return 投诉列
     */
    @Override
    public UserInformation selectUserInformationById(Long id)
    {
        return userInformationMapper.selectUserInformationById(id);
    }

    /**
     * 查询投诉列列表
     *
     * @param userInformation 投诉列
     * @return 投诉列
     */
    @Override
    public List<UserInformation> selectUserInformationList(UserInformation userInformation)
    {
        return userInformationMapper.selectUserInformationList(userInformation);
    }

    /**
     * 新增投诉列
     *
     * @param userInformation 投诉列
     * @return 结果
     */
    @Override
    public int insertUserInformation(UserInformation userInformation)
    {
        return userInformationMapper.insertUserInformation(userInformation);
    }

    /**
     * 修改投诉列
     *
     * @param userInformation 投诉列
     * @return 结果
     */
    @Override
    public int updateUserInformation(UserInformation userInformation)
    {
        return userInformationMapper.updateUserInformation(userInformation);
    }

    /**
     * 批量删除投诉列
     *
     * @param ids 需要删除的投诉列主键
     * @return 结果
     */
    @Override
    public int deleteUserInformationByIds(Long[] ids)
    {
        return userInformationMapper.deleteUserInformationByIds(ids);
    }

    /**
     * 删除投诉列信息
     *
     * @param id 投诉列主键
     * @return 结果
     */
    @Override
    public int deleteUserInformationById(Long id)
    {
        return userInformationMapper.deleteUserInformationById(id);
    }
}
